package captcha

import (
	"github.com/mojocn/base64Captcha"
	"image/color"
)

var store = base64Captcha.DefaultMemStore

// 验证码来源
var worldSource = map[int]string{
	1: base64Captcha.TxtNumbers,
	2: base64Captcha.TxtSimpleCharaters,
}

func Captcha(with, height, worldNum int, captType int) (id, encode string) {

	if _, ok := worldSource[captType]; ok == false {
		captType = 2
	}

	// 配置验证码的参数
	driverString := base64Captcha.DriverString{
		Height:          height,
		Width:           with,
		NoiseCount:      10, //干扰数
		ShowLineOptions: 2 | 4 | 8,
		Length:          worldNum,
		Source:          worldSource[captType],
		BgColor:         &color.RGBA{R: 3, G: 102, B: 214, A: 125},
		//BgColor: &color.RGBA{R: 40, G: 30, B: 89, A: 29},
		Fonts: []string{"wqy-microhei.ttc"},
	}

	// ConvertFonts 按名称加载字体
	driver := driverString.ConvertFonts()
	// 创建 Captcha
	captcha := base64Captcha.NewCaptcha(driver, store)

	// Generate 生成随机 id、base64 图像字符串
	id, b64s, err := captcha.Generate()
	if err != nil {
		return "", err.Error()
	}
	return id, b64s
}

func VerifyCaptcha(id string, VerifyValue string) bool {
	if store.Verify(id, VerifyValue, true) {
		//验证成功
		return true
	}
	return false
}
